/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;
using NUnit.Framework;
using Rhino.Mocks;

namespace NTasking.ValidationTests.ACATS_3_0.C9
{

    [TestFixture(Description = "")]
    public class C93005B : ValidationTest
    {

        private static TX tx = null;
        private static TY ty = null;
        private static TZ tz = null;


        private class TA : ValidationTestTask    // CHECKS THAT TX TERMINATES.
        {

            internal TA(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                TAB tab = null;

                Ada.Declare(() =>
                {
                    tab = new TAB(Result);
                },
                Begin: () =>
                {
                });

                try
                {
                    tx.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TA");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TA");
                    Result.TestFailed();
                }
            }

        }


        private class TB : ValidationTestTask    // CHECKS THAT TY TERMINATES
        {

            internal TB(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                TBB tbb = null;

                Ada.Declare(() =>
                {
                    tbb = new TBB(Result);
                },
                Begin: () =>
                {
                });

                try
                {
                    ty.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TB");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TB");
                    Result.TestFailed();
                }
            }

        }


        private class TC : ValidationTestTask
        {

            internal TC(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                TCB tcb = null;

                Ada.Declare(() =>
                {
                    tcb = new TCB(Result);
                },
                Begin: () =>
                {
                });

                try
                {
                    tz.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TC");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TC");
                    Result.TestFailed();
                }
            }

        }


        private class TAB : ValidationTestTask
        {

            internal TAB(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                try
                {
                    tx.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TAB");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TAB");
                    Result.TestFailed();
                }
            }

        }


        private class TBB : ValidationTestTask
        {

            internal TBB(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                try
                {
                    ty.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TBB");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TBB");
                    Result.TestFailed();
                }
            }

        }


        private class TCB : ValidationTestTask
        {

            internal TCB(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                try
                {
                    tz.E();
                    Console.WriteLine("RENDEZVOUS COMPLETED WITHOUT ERROR - TCB");
                    Result.TestFailed();
                }
                catch (TaskingError)
                {
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION - TCB");
                    Result.TestFailed();
                }
            }

        }


        private class TX : ValidationTestTask    // WILL NEVER BE ACTIVATED.
        {

            [Entry]
            internal Action E { get; private set; }


            internal TX(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                Console.WriteLine("TX ACTIVATED OK");
                Result.TestFailed();

                // In case of failure
                while (true)
                {
                    Select(s => s.
                        Accept(E).
                    Or.
                        Terminate
                    );
                }
            }

        }


        private class TY : ValidationTestTask
        {

            [Entry]
            internal Action E { get; private set; }


            internal TY(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                Console.WriteLine("TY ACTIVATED OK");
                Result.TestFailed();

                // In case of failure
                while (true)
                {
                    Select(s => s.
                        Accept(E).
                    Or.
                        Terminate
                    );
                }
            }

        }


        private class TZ : ValidationTestTask
        {

            [Entry]
            internal Action E { get; private set; }


            internal TZ(IValidationTestResult result)
                : base(result)
            {
            }


            protected override void Body()
            {
                Console.WriteLine("TZ ACTIVATED OK");
                Result.TestFailed();

                // In case of failure
                while (true)
                {
                    Select(s => s.
                        Accept(E).
                    Or.
                        Terminate
                    );
                }
            }

        }


        [Test(Description =
            "CHECK THAT WHEN AN EXCEPTION IS RAISED IN A DECLARATIVE PART, A TASK" +
            "DECLARED IN THE SAME DECLARATIVE PART BECOMES TERMINATED." +
            "" +
            "CHECK THAT A TASK WAITING ON ENTRIES OF SUCH A" +
            "TERMINATED-BEFORE-ACTIVATION TASK RECEIVES TASKING_ERROR." +
            "" +
            "THIS TEST CHECKS THE CASE IN WHICH SEVERAL TASKS ARE WAITING FOR" +
            "ACTIVATION WHEN THE EXCEPTION OCCURS.")]
        public void Test()
        {
            IValidationTestResult result = Mocks.DynamicMock<IValidationTestResult>();
            TA ta;
            TB tb;
            TC tc;

            Ada.Declare(() =>
            {
                ta = new TA(result);
                tb = new TB(result);
                tc = new TC(result);

                // Setup text expectations

                Expect.Call(result.TestFailed).Repeat.Never();
                Mocks.ReplayAll();

                // Start test execution
            },
            Begin: scope =>
            {
                try
                {
                    Ada.Declare(() =>
                    {
                        tx = new TX(result);
                        ty = new TY(result);
                        tz = new TZ(result);

                        // Ensure activation of ta, tb, tc, tab, tbb, tcb
                        scope.Activate();

                        // Prevent activation of tx, ty and tz
                        throw new NullReferenceException();
                    },
                    Begin: () =>
                    {
                    });
                }
                catch (NullReferenceException)
                {
                }
                catch (TaskingError)
                {
                    Console.WriteLine("TaskingError in main program");
                    result.TestFailed();
                }
                catch (Exception)
                {
                    Console.WriteLine("ABNORMAL EXCEPTION IN MAIN-1");
                    result.TestFailed();
                }
            });

            //using (new TaskingScope())
            //{
            //    ta = new TA(result);
            //    tb = new TB(result);
            //    tc = new TC(result);

            //    // Setup text expectations

            //    Expect.Call(result.TestFailed).Repeat.Never();
            //    Mocks.ReplayAll();

            //    // Start test execution

            //    try
            //    {
            //        using (new TaskingScope(() =>
            //        {
            //            tx = new TX(result);
            //            ty = new TY(result);
            //            tz = new TZ(result);

            //            // Ensure activation of ta, tb, tc, tab, tbb, tcb
            //            TaskType.Start(ta, tb, tc);

            //            // Prevent activation of tx, ty and tz
            //            throw new NullReferenceException();
            //        }))
            //        {
            //            Console.WriteLine("PACKAGE DIDN'T RAISE EXCEPTION");
            //            result.TestFailed();
            //        }
            //    }
            //    catch (NullReferenceException)
            //    {
            //    }
            //    catch (TaskingError)
            //    {
            //        Console.WriteLine("TaskingError in main program");
            //        result.TestFailed();
            //    }
            //    catch (Exception)
            //    {
            //        Console.WriteLine("ABNORMAL EXCEPTION IN MAIN-1");
            //        result.TestFailed();
            //    }

            //}
        }

    }

}
